#pragma once
#include "CWindow/CWindowBase.h"
#include "CBitmapDraw.h"
#include <vector>
#include <deque>
#include <mutex>
#include "CAudioClient.h"
#include <Xinput.h>

#pragma comment(lib,"Xinput.lib")

#define WM_DRAWAUDIO        (WM_USER + 20)

const int draw_mode_count = 6;
const int grid_x_count = 32;
const int grid_y_count = 30;

const DWORD DRAW_BACK_GRID_COLOR = RGB(200, 200, 200);
const DWORD DRAW_EMPTY_COLOR = RGB(255, 255, 255);
const DWORD DRAW_LINE_COLOR = RGB(255, 0, 0);
const DWORD DRAW_RECT_COLOR = RGB(255, 64, 0);
const DWORD DRAW_FILL_RECT_COLOR = RGB(0, 255, 255);
const DWORD DRAW_CIRCLE_COLOR = RGB(255, 0, 255);
const DWORD DRAW_GRID_COLOR = RGB(0, 128, 255);
const DWORD DRAW_FILL_GRID_COLOR = RGB(0, 128, 255);
const DWORD DRAW_PEN_COLOR = RGB(255, 0, 255);

typedef struct _DRAW_ITEM_DATA
{
    POINT ptLT;
    POINT ptRB;
    DWORD dwType;

    _DRAW_ITEM_DATA(POINT ptLT, POINT ptRB, DWORD dwType)
    {
        this->ptLT = ptLT;
        this->ptRB = ptRB;
        this->dwType = dwType;
    }

    std::vector<POINT> PointList;
}DRAW_ITEM_DATA;

class CMainFrame: 
    public CWindowBase
{
public:

    CMainFrame();
    CMainFrame(const CMainFrame& r) = delete;
    ~CMainFrame();

    HDC GetMemDC();
    HDC GetBackMemDC();
    HBITMAP GetBitmap();
    HBITMAP GetBackBitmap();

protected:
    wnd_msg LRESULT OnCreate(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnClose(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnEraseBkgnd(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnPaint(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnDestroy(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnMouseActivate(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnNcHittest(WPARAM wParam, LPARAM lParam);
    wnd_msg LRESULT OnDrawAudio(WPARAM wParam, LPARAM lParam);

private:

    void DrawAudioPoint(const std::deque<float>& left, const std::deque<float>& right);


    DECLARE_WND_MESSAGE_MAP()

private:

    HDC         m_hMemDC = NULL;
    HBITMAP     m_hMemBitmap = NULL;

    HDC         m_hBackMemDC = NULL;
    HBITMAP     m_hBackMemBitmap = NULL;
    HFONT       m_hFont = NULL;

    POINT       m_ptDown = {0};
    DWORD       m_dwType = 0;
    DWORD       m_dwCount = 1;

    bool        m_bGDIMode = false;
    bool        m_bDrag = false;
    bool        m_bDrawGrid = false;
    bool        m_bDrawTime = false;

    std::deque<float> m_WaveLeft;
    std::deque<float> m_WaveRight;
    std::mutex  m_lock;
    int         m_nDrawIndex = 0;
    CAudioClient m_capture;
    CBitmapDraw m_draw;
};